package com.pem.controller;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.pem.service.ProjectFileService;

@Controller
@RequestMapping(value = "/pem/projectfile")
public class ProjectFileController {
	
	@Autowired
	ProjectFileService projectFileService;

	@RequestMapping("/init")
    public String init() throws Exception {
		FileChannel fc =new FileInputStream("e:\\bp.json").getChannel();
		IntBuffer ib=fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();
		while(ib.hasRemaining()){
			ib.get();
		}
		if(fc!=null){
			fc.close();
		}
		return null;
    }
	
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new FileReader("e:\\bp.json"));// 读取原始json文件  
        String s = null, ws = null;  
        String str="";
        while ((s = br.readLine()) != null) {  
             str=str+s;
        }  
        try {  
        	Map map = JSONObject.parseObject(str, Map.class);
        	List<Map>  list=(List<Map>) map.get("RECORDS");
        	System.out.println(list.size());
        	String url="";
        	String filename="";
        	String path="";
        	int i=0;
        	for(Map<String,String> obj:list){
        		path=obj.get("path");
        		i=i+1;
        		if(i>=339){
        			url="http://58.68.234.140:6677/";
        			if(path.indexOf("/")<0){
        				path=path+"/";
        			}
        			url=url+path;
        			url=url+obj.get("filename");
        			filename=obj.get("name");
        			System.out.println("count:"+i+"===url:"+url+"===filename:"+filename);
        			try {
						downloadWechatLogo(url, filename);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        			Thread.sleep(2000);
        		}
        	}
        } catch (JSONException e) {  
            e.printStackTrace();  
        }  

        br.close();  
	}
	
	/**
	   * 下载文件到本地
	   *
	   * @param urlString 被下载的文件地址
	   * @param filename 本地文件名
	   * @throws Exception 各种异常
	   */
	public static void downloadWechatLogo(String urlString, String filename) throws Exception {
		String path = "d:/default_upload/projectfile/";
		String pathname=path+"/"+filename;
	    // 构造URL
	    URL url = new URL(urlString);
	    // 打开连接
	    URLConnection con = url.openConnection();
	    // 输入流
	    InputStream is = con.getInputStream();
	    // 1K的数据缓冲
	    byte[] bs = new byte[1024];
	    // 读取到的数据长度
	    int len;
	    // 输出的文件流
	    OutputStream os = new FileOutputStream(pathname);
	    // 开始读取
	    while ((len = is.read(bs)) != -1) {
	      os.write(bs, 0, len);
	    }
	    // 完毕，关闭所有链接
	    os.close();
	    is.close();
	}   
	
}
